﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Script.Serialization;
using System.Xml.Serialization;
using System.Security.Principal;
using Vit.Common;
using Vit.Web.Providers;
using Vit.Web;
using Vit.Web.Mvc.DataAnnotations;
using Vit.Web.Models;

namespace Vit.Web.Models
{
    [Operation("List", "UserRoles", 1, LinkUrlPattern = "/Admin/User/Roles/{Account}", CssClass = "dialog")]
    public class User : System.Security.Principal.IPrincipal
    {
        public User()
        {
            
            Roles = new List<Role>();
            Profiles = new List<Profile>();
            LastActiveTime = DateTime.Now;
            LastLoginTime = null;
            LastRequestUrl = HttpContext.Current.Request.RawUrl;
        }

        [StringLength(50)]
        [Required]
        [ShowFor(ShowForFilter = true)]
        public string Name { get; set; }
        [Key]
        [StringLength(50)]
        [ReadOnlyOn(false, true)]
        [UIHint("DialogLink", null, "url", "/Admin/User/Edit/{Account}")]
        [Remote("AccountExists", "User", ErrorMessage="Account_Duplicated" )]
        public string Account { get; set; }
        [StringLength(128)]
        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }
        [StringLength(200)]
        [DataType(DataType.EmailAddress)]
        [Remote("EmailExists", "User", ErrorMessage = "Email_Duplicated", AdditionalFields = "Account")]
        public string Email { get; set; }

        [ShowFor(false, true, false, false)]
        public DateTime? LastLoginTime { get; set; }
        [ShowFor(false, true, false, false)]
        public DateTime? LastActiveTime { get; set; }
        [StringLength(500)]
        [ShowFor(false, true, false, false)]
        public string LastRequestUrl { get; set; }
        [DataSource(typeof(Sex))]
        [UIHint("DataSource")]
        [FilterUIHint("DataSource")]
        [ShowFor(ShowForFilter = true)]
        public int? Sex { get; set; }
        [DataSource(typeof(DataStatus))]
        [UIHint("DataSource")]
        public int Status { get; set; }

        [ScriptIgnore]
        [XmlIgnore]
        public virtual WebSite Site { get; set; }
        [ScriptIgnore]
        [XmlIgnore]
        public virtual ICollection<Role> Roles { get; set; }
        [ScriptIgnore]
        [XmlIgnore]
        public virtual ICollection<Profile> Profiles { get; set; }

        public bool IsOnline()
        {
            return LastActiveTime.HasValue && LastActiveTime.Value.AddMinutes(30) >= DateTime.Now;
        }

        public bool IsAnonymous()
        {
            return string.IsNullOrEmpty(Account) || string.IsNullOrEmpty(Name);
        }

        #region IPrincipal Members

        [NotMapped]
        public IIdentity Identity
        {
            get { return new GenericIdentity(this.Account); }
        }

        public bool IsInRole(string role)
        {
            return this.Roles.Any(o => o.Name == role);
        }

        #endregion
    }
}
